package com.jackrain.nea.cp;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.ad.api.CgroupsFetchAllCmd;
import com.jackrain.nea.ad.api.CmenuReloadCmd;
import com.jackrain.nea.ad.api.CmenuTreeCmd;
import com.jackrain.nea.cp.api.*;
import com.jackrain.nea.utility.ServiceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lzm
 * @date 2017-11-15
 */
@Slf4j
@RestController
@Api(value = "CppermissionCtrl", description = "权限设置控制器")
public class CppermissionCtrl {

    @Reference(version = "1.0", group = "ad")
    private CmenuTreeCmd treeCmd;
    @Reference(version = "1.0", group = "ad")
    private CmenuReloadCmd menuReloadCmd;
    @Reference(version = "1.0", group = "cp")
    private CPermissionReloadCmd permissionReloadCmd;

    @RequestMapping(value = "/p/cs/getAllGroups", method = RequestMethod.GET)
    @ApiOperation(value = "所有分组列表", notes = "获取所有分组列表")
    public JSONObject getAllGroups(HttpServletRequest request){
        return ServiceUtil.invoke(CgroupsFetchAllCmd.class, request, null);
    }

    @RequestMapping(value = "/p/cs/reloadMenu", method = RequestMethod.GET)
    @ApiOperation(value = "菜单reload", notes = "重刷菜单树")
    public JSONObject reloadMenu(HttpServletRequest request){
        return ServiceUtil.invoke(CmenuReloadCmd.class, request, null);
    }

    @RequestMapping(value = "/p/cs/reloadPermission", method = RequestMethod.GET)
    @ApiOperation(value = "菜单权限reload", notes = "重刷菜单权限")
    public JSONObject reloadPermission(HttpServletRequest request){
        return ServiceUtil.invoke(CPermissionReloadCmd.class, request, null);
    }

    @RequestMapping(value = "/p/c/__test/reloadMenu", method = RequestMethod.GET)
    @ApiOperation(value = "菜单reload-测试", notes = "重刷菜单树-测试")
    public JSONObject reloadMenuForTest(HttpServletRequest request){
        return ServiceUtil.testInvoke(CmenuReloadCmd.class, request, null);
    }

    @RequestMapping(value = "/p/c/__test/reloadPermission", method = RequestMethod.GET)
    @ApiOperation(value = "菜单权限reload-测试", notes = "重刷菜单权限-测试")
    public JSONObject reloadPermissionForTest(HttpServletRequest request){
        return ServiceUtil.testInvoke(CPermissionReloadCmd.class, request, null);
    }

    @RequestMapping(value = "/p/c/__test/getMenuTree", method = RequestMethod.GET)
    @ApiOperation(value = "获取功能菜单（含动作定义）列表-测试", notes = "获取功能菜单（含动作定义）列表-测试")
    public JSONObject getMenuTreeForTest(HttpServletRequest request){
        return ServiceUtil.testInvoke(CmenuTreeCmd.class, request, null);
    }

    @RequestMapping(value = "/p/cs/getMenuTree", method = RequestMethod.GET)
    @ApiOperation(value = "获取功能菜单（含动作定义）列表", notes = "获取功能菜单（含动作定义）列表")
    public JSONObject getMenuTree(HttpServletRequest request){
        return ServiceUtil.invoke(CmenuTreeCmd.class, request, null);
    }

    @RequestMapping(value = "/p/cs/queryMenuPermission", method = RequestMethod.GET)
    @ApiOperation(value = "查询菜单权限列表", notes = "查询菜单权限列表")
    public JSONObject queryMenuPermission(HttpServletRequest request,
                                          @RequestParam(value = "param")  String param){
        return ServiceUtil.invoke(CmenuPermissionQueryCmd.class, request, param);
    }

    @RequestMapping(value = "/p/cs/queryActionPermission", method = RequestMethod.GET)
    @ApiOperation(value = "查询动作定义权限列表", notes = "查询动作定义权限列表")
    public JSONObject queryActionPermission(HttpServletRequest request,
                                            @RequestParam(value = "param")  String param){
        return ServiceUtil.invoke(CactionPermissionQueryCmd.class, request, param);
    }

    @RequestMapping(value = "/p/cs/savePermission", method = RequestMethod.POST)
    @ApiOperation(value = "权限保存", notes = "权限保存")
    public JSONObject savePermission(HttpServletRequest request,
                                     @RequestParam(value = "param")  String param){
        return ServiceUtil.invoke(CPermissionSaveCmd.class, request, param);
    }

    @RequestMapping(value = "/p/cs/getPermission", method = RequestMethod.GET)
    @ApiOperation(value = "获取权限", notes = "获取权限")
    public JSONObject getPermission(HttpServletRequest request,
                                     @RequestParam(value = "param")  String param){
        return ServiceUtil.invoke(CPermissionGetCmd.class, request, param);
    }

    @RequestMapping(value = "/p/cs/copyPermission" ,method = RequestMethod.POST)
    @ApiOperation(value = "复制权限",notes = "复制权限")
    public JSONObject copyPermission(HttpServletRequest request,@RequestParam(value = "param")String param){
        return ServiceUtil.invoke(CPermissionCopyCmd.class,request,param);
    }

    @RequestMapping(value = "/p/cs/fetchActionsInCustomizePage", method = RequestMethod.GET)
    @ApiOperation(value = "获取定制界面下的按钮", notes = "获取定制界面下的按钮")
    public JSONObject fetchActionsInCustomizePage(HttpServletRequest request,
                                    @RequestParam(value = "param")  String param){
        return ServiceUtil.invoke(CactionFetchInCustomizeCmd.class, request, param);
    }
}
